package com.manor.main.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.RegexUtils;

import com.manor.common.utils.ToastUtil;
import com.manor.main.R;

/**
 * 重置密码
 */
public class ResetPasswordFragment extends Fragment {


    public ResetPasswordFragment() {

    }

    public static ResetPasswordFragment newInstance() {
        return new ResetPasswordFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_reset_password, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EditText etFirstPassword = view.findViewById(R.id.et_password);
        EditText etConfirmPassword = view.findViewById(R.id.et_confirm_password);
        view.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String firstPassword = etFirstPassword.getText().toString().trim();
                String secondPassword = etConfirmPassword.getText().toString().trim();
                if (verify(firstPassword)) {
                    return;
                }

                if (verify(secondPassword)) {
                    return;
                }

                if (etConfirmPassword.equals(etFirstPassword)) {
                    ToastUtil.show("两次密码不一致,请重新输入");
                    return;
                }
                // TODO: 2021/1/6 进入设置密码页面
            }
        });
    }

    private boolean verify(String content) {
        if (TextUtils.isEmpty(content)) {
            ToastUtil.show("请输入密码");
            return true;
        }

        if (RegexUtils.isMatch(PasswordLoginFragment.REGEX_PSWD, content)) {
            ToastUtil.show("密码格式不正确");
            return true;
        }
        return false;
    }
}